package com.woniuxy.config;

import com.woniuxy.util.RabbitmqConst;
import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * @author ：CuiYC
 * @description：
 * @date ：Created in 2025/3/20 15:54
 * @modified By：
 */
@Component
public class RabbitmqConfig {

    @Bean
    public Queue checkOrderStatQueue(){
        return new Queue(RabbitmqConst.CHECK_ORDER_STAT_QUEUE);
    }
    @Bean
    public CustomExchange delayExchange(){
        Map map = new HashMap();
        //指定交换机类型
        //参数1：交换机类型
        //参数2：交换机基础类型，可写三种基础交换机的类型
        map.put("x-delayed-type","direct");

        /**
         * 参数1：交换机在rabbitmq服务器中的名字
         * 参数2：交换机类型
         * 参数3：是否持久化。当关闭mq服务时，该交换机是否还存在。默认持久化
         * 参数4：是否自动删除。当没有交换机和该交换机绑定时，是否自动删除该交换机。默认不删除
         * 参数5：其他参数（指定交换机类型）
         */
        return new CustomExchange(RabbitmqConst.DELAY_EXCHANGE,
                "x-delayed-message",
                true,false,map);
    }

    @Bean
    public Binding checkOrderStatQueueToDelayExchange(Queue checkOrderStatQueue,
                                                      CustomExchange delayExchange){
        return BindingBuilder.bind(checkOrderStatQueue)
                .to(delayExchange).with("delay").noargs();
    }


    /**
     * 秒杀订单队列
     */
    @Bean
    public Queue addKillBook(){
        return new Queue(RabbitmqConst.ADD_KILL_BOOK_QUEUE);
    }
    /**
     * 秒杀选用主题交换机
     */
    @Bean
    public TopicExchange topicExchange(){
        return new TopicExchange(RabbitmqConst.TOPIC_EXCHANGE);
    }
    @Bean
    public Binding addKillBookToTopicExchange(Queue addKillBook, TopicExchange topicExchange){
        return BindingBuilder.bind(addKillBook).to(topicExchange)
                .with("killBook");
    }

    /**
     * 展示秒杀书籍菜单的队列
     */
    @Bean
    public Queue showKillBook(){
        return new Queue(RabbitmqConst.SHOW_KILL_BOOK_QUEUE);
    }

    @Bean
    public Binding showKillBookToDelayExchange(Queue showKillBook, CustomExchange delayExchange){
        return BindingBuilder.bind(showKillBook).to(delayExchange)
                .with("showKillBook").noargs();
    }
}
